package org.ohos.rajawali3d.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import ohos.media.common.Source;
import ohos.media.player.Player;
import org.ohos.rajawali3d.ResourceTable;

import java.io.FileDescriptor;
import java.io.IOException;

public class VideoPlayAbilitySlice extends AbilitySlice implements SurfaceOps.Callback {
    private SurfaceProvider fragmentFrame;
    private Player mMediaPlayer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_video_play);
        fragmentFrame = new SurfaceProvider(getContext());
        final ComponentContainer.LayoutConfig params
                = new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT,
                DirectionalLayout.LayoutConfig.MATCH_PARENT);
        fragmentFrame.pinToZTop(true);
        fragmentFrame.setLayoutConfig(params);
        fragmentFrame.getSurfaceOps().get().addCallback(this);
        ShapeElement colorElement = new ShapeElement();
        colorElement.setRgbColor(new RgbColor(0xff00ddff));
        fragmentFrame.setBackground(colorElement);
        DirectionalLayout container = (DirectionalLayout) findComponentById(ResourceTable.Id_video_container);
        container.addComponent(fragmentFrame);
    }

    @Override
    public void onActive() {
        super.onActive();
        mMediaPlayer = new Player(getContext());
        ResourceManager resManager = getContext().getResourceManager();
        RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/sintel_trailer_480p.mp4");
        try {
            FileDescriptor fdx = rawFileEntry.openRawFileDescriptor().getFileDescriptor();
            mMediaPlayer.setSource(rawFileEntry.openRawFileDescriptor());
        } catch (IOException e) {
            e.printStackTrace();
        }
        mMediaPlayer.enableSingleLooping(true);

    }

    @Override
    protected void onInactive() {
        super.onInactive();
        mMediaPlayer.stop();
        mMediaPlayer.release();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void surfaceCreated(SurfaceOps surfaceOps) {
        Surface s = surfaceOps.getSurface();
        mMediaPlayer.setVideoSurface(s);
        mMediaPlayer.prepare();
        mMediaPlayer.play();
    }

    @Override
    public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceOps surfaceOps) {

    }
}
